package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.jms.Destination;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.ddyx.entity.Activity;
import com.ddyx.entity.EventAttenStore;
import com.ddyx.entity.Store;
import com.ddyx.entity.StoreBoardContent;
import com.ddyx.entity.StoreLogo;
import com.ddyx.entity.StoreTemplate;
import com.ddyx.mapper.ActivityMapper;
import com.ddyx.mapper.EventAttenStoreMapper;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.mapper.StoreBoardContentMapper;
import com.ddyx.mapper.StoreLogoMapper;
import com.ddyx.mapper.StoreMapper;
import com.ddyx.mapper.StoreTemplateMapper;
import com.ddyx.mapper.WxUserMapper;
import com.ddyx.mapper.user.UStoreMapper;
import com.ddyx.service.IStoreService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.util.Utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Slf4j
@Service
@Transactional
public class StoreServiceImpl implements IStoreService {
	
	private static ObjectMapper jsonMapper = new ObjectMapper();
	
	@Resource
	StoreLogoMapper storeLogoMapper;

	@Resource
	StoreBoardContentMapper storeBoardContentMapper;

	@Resource
	StoreMapper storeMapper;
	@Resource
	UStoreMapper usMapper;
	
	@Resource
	EventAttenStoreMapper eventAttenStoreMapper;
	
	@Resource
	GoodsMapper goodsMapper;

	@Resource
	WxUserMapper wxUserMapper;
	
	@Resource
	StoreTemplateMapper storeTemplateMapper;
	
	@Resource
	JmsTemplate jmsTemplate;
	@Resource
	Destination tmpMsgDestination;
	
	@Resource
	ActivityMapper activityMapper;
	
	/*
	 * 专门用于获取门店信息
	 */
	/*@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getStoreDetails(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Store bean = new Store();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			MyBeanUtils.populate(bean, paramMap);
			bean = storeMapper.selectOne(bean);
			result.setData(bean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}*/

	/*
	 * 获取多门店首页 多门店商品展示信息
	 */
	/*@Override
	public ResponseForm getGoodsShow(RequestForm param) {
		ResponseForm result = new ResponseForm();
		// Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			Goods goods = new Goods();
//			List<Store> StoreList = storeMapper.select(new Store());
			List<LinkedHashMap> StoreList = usMapper.selectAllBySelf();
			for (LinkedHashMap store : StoreList) {
				goods.setStoreNo((String)store.get("storeNo"));
//				RowBounds rowBounds = new RowBounds(1, 4);
				List<Goods> goodsList = goodsMapper.selectByRowBounds(goods, new RowBounds(1, 4));
				store.put("goodsList",goodsList);
				goods.setStoreNo(null);// 置空
			}
			result.setData(StoreList);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}*/

//	@Transactional
//	@SuppressWarnings("unchecked")
//	@Override
//	public ResponseForm addStore(RequestForm param) {
//		ResponseForm result = new ResponseForm();
//		if (null != param) {
//			Store bean = new Store();
//			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
//			try {
//				MyBeanUtils.populate(bean, paramMap);
//				bean.setCreateTime(new Date());
//				String storeLogo = bean.getStoreLogo();
//				String storeBoardContent = bean.getStoreBoardContent();
//				
//				// 小店编号 
//				String storeNo = null;
//				storeNo =checkStroreNo();
//				
//				if(storeNo==null){
//					result.setStatus(false);
//					result.setMessage("操作有误,请重试.");
//					log.error("storeNo有误");
//					return result;
//				}
//				
//				bean.setStoreNo(storeNo);
//				storeMapper.insertSelective(bean);
//				// 生成的小店id
//				Integer id = bean.getStoreId();
//				
//				if (null != id) {
//					
//					//模板
//					StoreTemplate st=new StoreTemplate();
//					st.setStoreNo(storeNo);
//					st.setTemplateId(bean.getTemplateId());
//					storeTemplateMapper.insertSelective(st);
//					
//					// logo图片
//					if (StringUtils.hasText(storeLogo)) {
//							StoreLogo bean1 = new StoreLogo();
//							bean1.setStoreId(id);
//							bean1.setStoreNo(storeNo);
//							bean1.setCreateTime(new Date());
//							bean1.setImageUrl(storeLogo);
//							storeLogoMapper.insertSelective(bean1);
//					}
//					// 门店背景图
//					if (StringUtils.hasText(storeBoardContent)) {
//							StoreBoardContent bean2 = new StoreBoardContent();
//							String suffix = storeBoardContent.substring(storeBoardContent.lastIndexOf(".") + 1);
//							int ii = Utils.JudgeFileType(suffix);
//							bean2.setMediaType(String.valueOf(ii));
//							bean2.setCreateTime(new Date());
//							bean2.setMediaUrl(storeBoardContent);
//							bean2.setStoreId(id);
//							bean2.setStoreNo(storeNo);
//							storeBoardContentMapper.insertSelective(bean2);
//					}
//					//启动页 新增一条记录
//					Activity activity = new Activity();
//					activity.setCreateTime(new Date());
//					activity.setName(bean.getStoreName()+"启动页");
//					activity.setType(1);
//					activity.setImgUrl("http://p1az9gcpw.bkt.clouddn.com/201801181200.jpg");
//					activity.setLink("1");
//					activity.setTime(4);
//					activity.setSkip(1);
//					activity.setStoreNo(storeNo);
//					activity.setSort(6);
//					activityMapper.insertSelective(activity);
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//				result.setStatus(false);
//				result.setMessage("操作有误");
//				log.error("操作有误");
//			}
//			return result;
//		}
//		return null;
//	}

//	@Transactional
//	@SuppressWarnings("unchecked")
//	@Override
//	public ResponseForm updateStore(RequestForm param) {
//		ResponseForm result = new ResponseForm();
//		if (null != param) {
//			Store bean = new Store();
//			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
//			try {
//				MyBeanUtils.populate(bean, paramMap);
//				bean.setUpdateTime(new Date());
//				storeMapper.updateByPrimaryKeySelective(bean);
////				String storeLogo = bean.getStoreLogo();
////				String storeBoardContent = bean.getStoreBoardContent();
////				Integer in = bean.getStoreId();
//				String storeNo = bean.getStoreNo();
//				if (StringUtils.hasText(storeNo)) {
//					//门店模板
//					
//					//模板
//					StoreTemplate sTemplate=new StoreTemplate();
//					sTemplate.setStoreNo(storeNo);
//					sTemplate.setTemplateId(bean.getTemplateId());
//					Example tExample = new Example(StoreTemplate.class);
//					Criteria tec=tExample.createCriteria();
//					tec.andEqualTo("storeNo", storeNo);
//					storeTemplateMapper.updateByExampleSelective(sTemplate, tExample);
//					
//					if(StringUtils.hasText(bean.getStoreLogo())){
//						//门店logo
//						StoreLogo so = new StoreLogo();
//						so.setStoreNo(storeNo);
//						so.setImageUrl(bean.getStoreLogo());
//						so.setUpdateTime(new Date());
//						Example example = new Example(StoreLogo.class);
//						Criteria c=example.createCriteria();
//						c.andEqualTo("storeNo", storeNo);
//						storeLogoMapper.updateByExampleSelective(so, example);
//					}
//					if(StringUtils.hasText(bean.getStoreBoardContent())){
//					// 门店背景图
//						StoreBoardContent st = new StoreBoardContent();
//						st.setStoreNo(storeNo);
//						String storeContent = bean.getStoreBoardContent().trim();
//						String suffix = storeContent.substring(storeContent.lastIndexOf(".") + 1);
//						int ii = Utils.JudgeFileType(suffix);
//						st.setStoreNo(storeNo);
//						st.setMediaType(String.valueOf(ii));
//						st.setCreateTime(new Date());
//						st.setMediaUrl(storeContent);
//						Example example1 = new Example(StoreLogo.class);
//						Criteria c1=example1.createCriteria();
//						c1.andEqualTo("storeNo", storeNo);
//						storeBoardContentMapper.updateByExampleSelective(st, example1);
//					}
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//				result.setStatus(false);
//				result.setMessage("操作有误");
//				log.error("操作有误");
//			}
//			return result;
//		}
//		return null;
//	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getStoreById(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			Store bean = new Store();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			try {
				MyBeanUtils.populate(bean, paramMap);
				Store bea = storeMapper.selectOne(bean);
				if (null != bea) {
					StoreLogo bean1 = new StoreLogo();
					bean1.setStoreNo(bean.getStoreNo());
					
					List<StoreLogo> list1 = storeLogoMapper.select(bean1);
					if (null != list1 && list1.size() > 0) {
						bea.setStoreLogo(list1.get(0).getImageUrl());
					}

					StoreBoardContent bean2 = new StoreBoardContent();
					bean2.setStoreNo(bean.getStoreNo());
					List<StoreBoardContent> list2 = storeBoardContentMapper.select(bean2);

					if (null != list2 && list2.size() > 0) {
						bea.setStoreBoardContentList(list2);
						bea.setStoreBoardContent(list2.get(0).getMediaUrl());
					}
					
					//获取当前小店的模版信息
					StoreTemplate storeTemplate = new StoreTemplate();
					storeTemplate.setStoreNo(bean.getStoreNo());
					storeTemplate = storeTemplateMapper.selectOne(storeTemplate);
					bea.setStoreTemplate(storeTemplate);
					bea.setTemplateId(storeTemplate.getTemplateId());
					
					//获取是否关注当前门店的状态
					EventAttenStore eventAttenStore = new EventAttenStore();
					eventAttenStore.setWxUid((String) paramMap.get("wxUid"));
					eventAttenStore.setStoreNo(bean.getStoreNo());
					Integer attenStatus = eventAttenStoreMapper.selectCount(eventAttenStore);
					bea.setAttenStatus(attenStatus);
				}
				result.setData(bea);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	/*
	 * 导购服务通知
	 */
	/*@SuppressWarnings("unchecked")
	@Override
	public void guideServiceNotification(RequestForm param) throws Exception {
		if (param.getData() != null) {
			List list = new ArrayList<>();
			TemplateMessageParam tmpMsgParam = new TemplateMessageParam();
			Map<String, Object> keyword = new HashMap<>();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			String wxUid = (String) paramMap.get("wxUid");
			String nickName = (String) paramMap.get("nickName");
			String storeNo = (String) paramMap.get("storeNo");
			String formId = (String) paramMap.get("formId");

			Store store = new Store();
			store.setStoreNo(storeNo);
			store = storeMapper.selectOne(store);

			WxUser wxUser = new WxUser();
			wxUser.setWxUid(wxUid);
			wxUser = wxUserMapper.selectOne(wxUser);
			
			if(wxUser!=null&&store!=null) {
				list.add(new KeyWord(nickName, EventTag.BLACK));
				list.add(new KeyWord(store.getStoreName(), EventTag.BLACK));
				list.add(new KeyWord(store.getLocation(), EventTag.BLACK));
				list.add(new KeyWord(store.getPhoneNumber(), EventTag.BLACK));
				int i=1;
				for (Object o : list) {
					keyword.put("keyword"+i, o);
					i++;
				}
				tmpMsgParam.setTouser(wxUser.getOpenId());
				tmpMsgParam.setForm_id(formId);
				tmpMsgParam.setTemplate_id(PropertiesUtil.getProperty(EventTag.GUIDETEMP));
				tmpMsgParam.setData(keyword);
				tmpMsgParam.setEmphasis_keyword("keyword"+2+".DATA");
				
				String json = jsonMapper.writeValueAsString(tmpMsgParam);
				//MQ异步发送 
				MQUtil.sendJsonMsg(json, jmsTemplate, tmpMsgDestination);
				
			}
			

		}else{
			log.error("参数有误");
			System.out.println("参数有误");
		}
	}*/
	
	//生成storeNo
	public static String StoreNo(String oldStroreNo){
		String temp = oldStroreNo.substring(1);
		//其中0表示补零而不是补空格，7表示至少7位  
		String StoreNo =String.format("%07d",Integer.parseInt(temp)+1);
		return "A"+StoreNo;
	}
	
	public String checkStroreNo(){
		
		String storeNo=null;
		
		PageHelper.startPage(1, 1, "store_id DESC");
		
		List<Store> list = storeMapper.selectAll();	
		
		if(null!=list && list.size()>0){
			System.out.println(list.get(0).getStoreNo());
			String oldStoreNo=list.get(0).getStoreNo();
			storeNo =StoreNo(oldStoreNo);
		}
		
		return storeNo;
		
		
	}
}
